package com.xdtech.hotel.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.hotel.service.DeviceOperationService;
import com.xdtech.hotel.vo.DeviceOperationItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-04-23 22:10:38
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class DeviceOperationController {
	@Autowired
	private DeviceOperationService deviceOperationService;
	@RequestMapping(value="/deviceOperation.do",params = "deviceOperation")
	public ModelAndView skipDeviceOperation() {
		return new ModelAndView("hotel/deviceOperation/deviceOperation_ftl");
	}
	
	@RequestMapping(value="/deviceOperation.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(DeviceOperationItem item,Pagination pg) {
		Map<String, Object> results =  deviceOperationService.loadPageDataByConditions(pg,item,"findDeviceOperationByCondition");
		return results;
	}
	
	@RequestMapping(value="/deviceOperation.do",params="loadListByLinkageId")
	@ResponseBody
	public Map<String, Object> loadListByLinkageId(Long linkageId) {
		Map<String, Object> results = deviceOperationService.loadListByLinkageId(linkageId);
//		results.put("total", items.size());
//		results.put("rows", items);
////		List<Long> selectIds = deviceOperationService.loadDeviceOperationItem(deviceOperationId)
//		results.put("selects", value)
		return results;
	}
	
	@RequestMapping(value="/deviceOperation.do",params = "editDeviceOperation")
	public ModelAndView editDeviceOperation(HttpServletRequest request,Long deviceOperationId) {
		if (deviceOperationId!=null) {
			request.setAttribute("deviceOperationItem", deviceOperationService.loadDeviceOperationItem(deviceOperationId));
		}
		return new ModelAndView("hotel/deviceOperation/editDeviceOperation_ftl");
	}
	
	@RequestMapping(value="/deviceOperation.do",params = "saveDeviceOperation")
	@ResponseBody
	public ResultMessage saveDeviceOperation(DeviceOperationItem item) {
		ResultMessage r = new ResultMessage();
		if (deviceOperationService.saveOrUpdateDeviceOperation(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/deviceOperation.do",params = "deleteDeviceOperationItems")
	@ResponseBody
	public ResultMessage deleteDeviceOperationItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> deviceOperationIds = new ArrayList<Long>();
			for (String id : tempIds) {
				deviceOperationIds.add(Long.valueOf(id));
			}
			deviceOperationService.deleteDeviceOperationInfo(deviceOperationIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

}
